package com.iyao_app.adapter;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.CommentData;
import com.iyao_app.util.TimeFormat;

import java.util.List;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/11.
 */

public class CommentAdapter extends BaseQuickAdapter<CommentData, BaseViewHolder> {


    public CommentAdapter(@Nullable List<CommentData> data) {
        super(R.layout.item_comment, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, CommentData item) {
        SimpleDraweeView sdvHead = helper.getView(R.id.sdv_head);
        TextView tvName = helper.getView(R.id.tv_name);
        TextView tvContent = helper.getView(R.id.tv_content);
        TextView tvTime = helper.getView(R.id.tv_time);
        ImageView ivComment = helper.getView(R.id.iv_comment);
        LinearLayout linRoot = helper.getView(R.id.lin_root);
        linRoot.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        try {
            tvName.setText(item.nickname);
            tvContent.setText(item.commentText);
            tvTime.setText(TimeFormat.getTime(item.createTime));
            ivComment.setVisibility(View.GONE);
            if (TextUtils.isEmpty(item.avatarUrl)) {
                sdvHead.setImageResource(R.mipmap.ic_morentouxiang);
            } else {
                Uri uri = Uri.parse(item.avatarUrl);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(80, 80))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(sdvHead.getController())
                        .setImageRequest(request)
                        .build();
                sdvHead.setController(controller);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
